package hello.app;

import hello.app.model.HelloApi;
import hello.app.model.HelloContactModel;
import hello.app.model.ModelManager;
import hello.app.model.data.HelloContact;
import hello.app.util.Debug;
import hello.app.util.Util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.widget.Toast;

public class EvaluationActicity extends Activity implements DialogInterface.OnClickListener {

	static final String MESSAGE = "評価してしてください";
	private HelloContact mContact = null;
	static OnCancelListener sOnCancelListener = null;
	public static final String NEW_CONTACT = "NEW_CONTACT";
	private ModelManager mManager;

	public static void start(HelloContact contact, Context context){

		Intent in = new Intent(context,EvaluationActicity.class);
		in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		in.putExtra("contact", contact);
		context.startActivity(in);
	}

	public static void start(HelloContact contact, Context context, OnCancelListener onFinishListener){
		EvaluationActicity.start(contact, context);
		sOnCancelListener = onFinishListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			try {
				mContact = (HelloContact) extras.getSerializable("contact");
				mManager = ModelManager.getInstatce();
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle(Application.TITLE);
				builder.setMessage(MESSAGE);
				builder.setPositiveButton(getString(R.string.good), this);
				builder.setNegativeButton(getString(R.string.bad), this);
				builder.setNeutralButton(getString(R.string.neutral), this);

				builder.setOnCancelListener(sOnCancelListener);
				AlertDialog alert = builder.create();
				alert.show();
			} catch (Exception e) {
				Debug.log(e.getMessage());
			}
		}else{
			finish();
		}
	}

	@Override
	protected void onDestroy() {
		mContact = null;
		super.onDestroy();
	}

	public void onClick(DialogInterface dialog, int which) {
		HelloApi api = mManager.getHelloApi();
		if(mContact == null || mContact.getId() == 0){
			return ;
		}
		try{
			Toast t = null;
			HelloContactModel contactModel = mManager.getHelloContactModel();
			HelloContact data;
			switch(which){
			case DialogInterface.BUTTON_POSITIVE:
				Debug.log("Call plusGoodCount");
				data = api.plusGoodCount(String.valueOf(mContact.getId()));
				//ローカルにある名前を優先する
				data = Util.protectContactName(contactModel, data);
				contactModel.save(data);
				t = Toast.makeText(
							EvaluationActicity.this, "高評価を行いました",
							Toast.LENGTH_LONG);
				t.show();
				break;
			case DialogInterface.BUTTON_NEGATIVE:
				Debug.log("Call plusBadCount");
				data = api.plusBadCount(String.valueOf(mContact.getId()));
				//ローカルにある名前を優先する
				data = Util.protectContactName(contactModel, data);
				contactModel.save(data);
				t = Toast.makeText(
							EvaluationActicity.this, "低評価を行いました",
							Toast.LENGTH_LONG);
				t.show();
				break;
			case DialogInterface.BUTTON_NEUTRAL:
				break;
			}

		}catch(Exception e){
			Toast t = Toast.makeText(
					EvaluationActicity.this, "エラーが起きました",
					Toast.LENGTH_LONG);
			t.show();
			Debug.error(e.getMessage());
		}finally{
			dialog.cancel();
			EvaluationActicity.this.finish();
		}
	}
}
